package com.share.shareplatform.sharegoods.controller;



import com.share.shareplatform.pojo.GoodsOneComment;
import com.share.shareplatform.pojo.GoodsTwoComment;
import com.share.shareplatform.sharegoods.service.GoodsCommentService;
import com.share.shareplatform.util.AuthorityUtil;
import com.share.shareplatform.util.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.PermitAll;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/goodsComment")
public class GoodsCommentController {

    @Autowired
    private GoodsCommentService goodsCommentService;

    @ApiOperation(value="添加一级评论",notes = "必需字段：goodsId,message")
    @PostMapping("/one")
    @PreAuthorize("hasAuthority('goodsCommentAdd')")
    public Result addOneOne(@RequestBody @Validated GoodsOneComment goodsOneComment) {
        Long userId = AuthorityUtil.getUserId();
        goodsOneComment.setUserId(userId);
        GoodsOneComment.setInit(goodsOneComment);
        Long flag = goodsCommentService.addOneOne(goodsOneComment);
        if(flag==0){
            return Result.getInstance(505,"发表评论失败");
        }

        return Result.getInstance(200,"success");
    }

    @ApiOperation(value="添加二级评论",notes = "必需字段：goodsId,message,replyId\n可选字段：replyUserId")
    @PostMapping("two")
    @PreAuthorize("hasAuthority('goodsCommentAdd')")
    public Result addOneTwo(@RequestBody @Validated GoodsTwoComment goodsTwoComment) {
        Long userId = AuthorityUtil.getUserId();
        goodsTwoComment.setUserId(userId);
        goodsTwoComment.setTime(new Timestamp(System.currentTimeMillis()));
        Long flag = goodsCommentService.addOneOTwo(goodsTwoComment);
        if(flag==0){
            return Result.getInstance(505,"发表评论失败");
        }

        return Result.getInstance(200,"success");
    }

    @ApiOperation(value="删除一级评论")
    @DeleteMapping("/one/{id}")
    @PreAuthorize("hasAuthority('goodsCommentDelete')")
    public Result deleteOneOneById(@PathVariable Long id) {
        Integer flag = goodsCommentService.deleteOneOneForeignById(id);
        if(flag==0){
            return Result.getInstance(505,"删除失败，评论已不存在");
        }
        return Result.getInstance(200,"删除了"+flag+"条评论");
    }

    @ApiOperation(value="删除二级评论")
    @DeleteMapping("/two/{id}")
    @PreAuthorize("hasAuthority('goodsCommentDelete')")
    public Result deleteTwoOneById(@PathVariable("id") Long id) {
        Integer flag = goodsCommentService.deleteTwoOneById(id);
        if(flag==0){
            return Result.getInstance(505,"删除失败，评论已不存在");
        }
        return Result.getInstance(200,"删除了"+flag+"条评论");
    }

    @ApiOperation(value="批量删除一级评论",notes = "其中ids为 'id-id-id' 这种类型的字符串")
    @DeleteMapping("/one/all/{ids}")
    @PreAuthorize("hasAuthority('goodsCommentDelete')")
    public Result deleteAllOneByIds(@PathVariable("ids") String ids) {
        String[] stringIds=ids.split("-");
        ArrayList<Long> arrayList=new ArrayList<>();
        for(String id:stringIds){
            arrayList.add(Long.parseLong(id));
        }

        Integer flag = goodsCommentService.deleteAllOneByIds(arrayList.toArray(new Long[arrayList.size()]));
        if(flag==0){
            return Result.getInstance(505,"删除失败，评论已不存在");
        }
        return Result.getInstance(200,"删除了"+flag+"条评论");
    }

    @ApiOperation(value="批量删除二级评论",notes = "其中ids为 'id-id-id' 这种类型的字符串")
    @DeleteMapping("/two/all/{ids}")
    @PreAuthorize("hasAuthority('goodsCommentDelete')")
    public Result deleteAllTwoByIds(@PathVariable("ids") String ids) {
        String[] stringIds=ids.split("-");
        ArrayList<Long> arrayList=new ArrayList<>();
        for(String id:stringIds){
            arrayList.add(Long.parseLong(id));
        }

        Integer flag = goodsCommentService.deleteAllTwoByIds(arrayList.toArray(new Long[arrayList.size()]));
        if(flag==0){
            return Result.getInstance(505,"删除失败，评论已不存在");
        }
        return Result.getInstance(200,"删除了"+flag+"条评论");
    }

    @ApiOperation(value="获取某个商品的所有一级评论")
    @GetMapping("/goods/{id}")
    @PermitAll
    public Result findAllOneByGoodsId(@PathVariable Long id) {
        List<GoodsOneComment> allByGoodsId = goodsCommentService.findAllOneByGoodsId(id);
        return Result.getInstance(200,"success").put("list",allByGoodsId);
    }

    @ApiOperation(value="获取某个商品的某条评论的二级评论")
    @GetMapping("/goods/{goodsId}/{replyId}")
    @PermitAll
    public Result findAllTwoByGoodsIdAndReplyId(
            @PathVariable("goodsId") Long goodsId,@PathVariable("replyId") Long replyId) {
        List<GoodsTwoComment> list = goodsCommentService.findAllTwoByGoodsIdAndReplyId(goodsId, replyId);
        return Result.getInstance(200,"success").put("list",list);
    }

    @ApiOperation(value="获取一级评论，按id")
    @GetMapping("/one/{id}")
    @PermitAll
    public Result findOneOneById(@PathVariable Long id) {
        GoodsOneComment oneOneById = goodsCommentService.findOneOneById(id);
        return Result.getInstance(200,"success").put("goodsComment",oneOneById);
    }

    @ApiOperation(value="获取二级评论，按id")
    @GetMapping("/two/{id}")
    @PermitAll
    public Result findOneTwoById(@PathVariable Long id) {
        GoodsTwoComment oneTwoById = goodsCommentService.findOneTwoById(id);
        return Result.getInstance(200,"success").put("goodsComment",oneTwoById);
    }
}
